function addProvider(obj){
	//ajax POST to add a provider to database.
	$.ajax({
		type:'POST', url:'api.cfm?api=add_provider', dataType: 'json', async: true, data: obj,
		success: function() {getProviders(); return false;},
		complete: function(){
			clearElements();			
			return false;
		},
		error: function(){return false;}
	});	
}


function editProvider(obj){
	//ajax POST to add a provider to database.
	$.ajax({
		type:'POST', url:'api.cfm?api=edit_provider', dataType: 'json', async: true, data: obj,
		success: function() {getProviders(); return false;},
		complete: function(){
			clearElements();			
			return false;
		},
		error: function(){return false;}
	});	
}



function deleteProvider(obj){
	//ajax POST to delete a provider to database.
	$.ajax({
		type:'POST', url:'api.cfm?api=delete_provider', dataType: 'json', async: false, data: obj,
		success: function() {return false;},
		complete: function(){ return false;},
		error: function(){return false;}
	});	
	clearElements();
	getProviders();
}


function getProviders(){
	//ajax get all providers to display in a dropdown.
	var data = "";
	$.ajax({
		type:'GET', url:'api.cfm?api=all_providers', dataType: 'json', async: false,
		success: function(json) {			
			$("#provider_tbl_body").html("");
			$("#provider_list").html("");
			data = json.DATA;
		},
		complete: function(){
			$("#provider_list").append('<option value="-1"> </option>');
			$.each(data, function(n, i){				
				$("#provider_list").append("<option class='"+ i[3] +
					"' value='" + i[2] + "'>" + i[0] + ", " + i[1] + "</option>");
				$("#provider_tbl_body").append("<tr><td>" + i[0] + 
					", " + i[1] + "</td><td>" + ( (i[3]) ? 'Yes' : 'No') + "</td></tr>");
			});			
			return false;
		},
		error: function(){return false;}
	});	
}


function bindElements(){
		
	//get all providers that are not deleted
	getProviders();
	
	//populates a textbox with selected drop downs information
	$("#provider_list").change(function(){
		if( $("#provider_list option:selected").val() == -1 ){
			//hide update and delete provider buttons.  Show Add button.
			$("#update_btn, #delete_btn").hide();
			$("#add_provider_btn").show();
			clearElements();
		}else{
			var provider = getDataById('get_provider', $("#provider_list option:selected").val() ).DATA;			
			$("#first_name").val(provider['PROVIDERFIRSTNAME']);
			$("#last_name").val(provider['PROVIDERLASTNAME']);
			$("#providerid").val(provider['PROVIDERID']);
			( ( provider['PROVIDERACTIVE'] == 'true' ) ? $("#active").attr({'checked':'checked'}) :$("#active").removeAttr('checked') );
			$("#update_btn, #delete_btn").show();
			$("#add_provider_btn").hide();
		}
	});
	
	//binding add button to save function, after data is valid.
	$("#add_provider_btn").bind("click", function(){
		var mydata = {
			firstName: $("#first_name").val(),
			lastName: $("#last_name").val(),
			active: ( ($("#active").attr('checked')) ? true : false )
		};
		addProvider(mydata);
	});
	
	//binding udpate button to update function
	$("#update_btn").bind("click", function(){
		var mydata = {
			firstName: $("#first_name").val(),
			lastName: $("#last_name").val(),
			active: ( ($("#active").attr('checked')) ? true : false ),
			db_type: 'update',
			id: $("#providerid").val()
		};
		editProvider(mydata);			  
	});	
	
	//binding delete button to delete function
	$("#delete_btn").bind("click", function(){
		var mydata = {
			firstName: $("#first_name").val(),
			lastName: $("#last_name").val(),
			active: ( ($("#active").attr('checked')) ? true : false ),
			db_type: 'delete',
			id: $("#providerid").val()
		};
		deleteProvider(mydata);				  
	});
}


function clearElements(){
	//resets all elements on page.
	$("#first_name, #last_name").val("");
	$("#active").removeAttr('checked');
	$("select").each( function(){
		// set its value to its first option
		$(this).val( $("#" + $(this).attr("id") + " option:first").val() );
	});
	//show add new provider buttons by default
	$("#update_btn, #delete_btn").hide();
	$("#add_provider_btn").show();	
}


//ajax GET to retrive data from database.
var getDataById = function(api, id){
	var data = {};
	var api_url = "api.cfm?api=" + api + ( (id) ? "&id=" + id : "" );
	
	if (id != -1){
		$.ajax({
		type:'GET', url: api_url, dataType: 'json', async: false,
			success: function(json) { data = json; }
		});
	}
	
	return data;
};


$(function(){
	clearElements();
	bindElements();
});